﻿// <copyright file="WaveWindowViewModel.cs" company="Waveform Lab.NET">
// Signal processing by PC oscilloscope
// http://waveformlab.codeplex.com/
//
// Copyright (c) 2011 Waveform Lab.NET
//
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
// </copyright>

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Windows.Input;
using System.Windows.Controls;
using PluginModule.Core;
using PluginModule.Core.MVVM;

namespace WaveformLab
{
    public class WaveWindowViewModel
    {
        private List<ModuleAttribute> _ApplicationModules;
        private List<ModuleAttribute> _DeviceModules;
        private CompositionContainer _ModuleComposionContainer;
        private string ModuleDirectory = @".\"; 
        private ICommand _ModuleApplicationSelectCommand;
        private ICommand _ModuleDeviceSelectCommand;

        public WaveWindowViewModel()
        {
            _ApplicationModules = new List<ModuleAttribute>();
            _ApplicationModules.Add(new ModuleAttribute { ModuleId = 2, ModuleName = "FFTModule" });
            _ApplicationModules.Add(new ModuleAttribute { ModuleId = 3, ModuleName = "F2fModule" });


            _DeviceModules = new List<ModuleAttribute>();
            _DeviceModules.Add(new ModuleAttribute { ModuleId = 1, ModuleName = "AudioModule" });
        }

        private CompositionContainer GetModuleAttribute()
        {
            var objCatalog = new AggregateCatalog();
            var objAssembly = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
            objCatalog.Catalogs.Add(objAssembly);
            objCatalog.Catalogs.Add(new DirectoryCatalog(ModuleDirectory));
            var container = new CompositionContainer(objCatalog);
            return container;
        }

        public bool CheckModule()
        {
            _ModuleComposionContainer = GetModuleAttribute();

            try
            {
                _ModuleComposionContainer.ComposeParts(this);
            }
            catch (Exception)
            {
                System.Windows.MessageBox.Show("Unable to Retrieve the Module Attribute");
                return false;
            }
            return true;
        }

        [ImportMany]
        public Lazy<IPlugin, IDictionary<string, object>>[] ModulesPurchased { get; set; }

        public List<ModuleAttribute> ApplicationModules
        {
            get
            {
                return _ApplicationModules;
            }
        }

        public List<ModuleAttribute> DeviceModules
        {
            get
            {
                return _DeviceModules;
            }
        }

        public System.Windows.Controls.ContentControl ModuleApplicationContainer { get; set; }
        public System.Windows.Controls.ContentControl ModuleDeviceContainer { get; set; }

        public string ModuleApplicationName
        {
            get 
            {
                IPlugin obj = (IPlugin)ModuleApplicationContainer.Content;
                if(obj==null) return string.Empty;
                else return obj.ModuleName;
            }
        }

        public string ModuleDeviceName
        {
            get
            {
                IPlugin obj = (IPlugin)ModuleDeviceContainer.Content;
                if (obj == null) return string.Empty;
                else return obj.ModuleName;
            }
        }

        public ICommand ModuleApplicationSelectCommand
        {
            get
            {
                if (_ModuleApplicationSelectCommand == null)
                {
                    _ModuleApplicationSelectCommand = new RelayCommand(ModuleSelectApplicationExecute, CanModuleSelectExecute);
                }
                return _ModuleApplicationSelectCommand;
            }
        }

        public ICommand ModuleDeviceSelectCommand
        {
            get
            {
                if (_ModuleDeviceSelectCommand == null)
                {
                    _ModuleDeviceSelectCommand = new RelayCommand(ModuleSelectDeviceExecute, CanModuleSelectExecute);
                }
                return _ModuleDeviceSelectCommand;
            }
        }

        private void ModuleSelectApplicationExecute(object param)
        {
            //   ModuleContainer.Content = new System.Windows.Controls.Button() { Content = "ClickMe" };

            foreach (var item in this.ModulesPurchased)
            {
                if (param.ToString().Equals(item.Metadata["ModuleId"].ToString()))
                {
                    UserControl uc = item.Value as UserControl;
                    if (uc != null)
                    {
                        ModuleApplicationContainer.Content = null;
                        ModuleApplicationContainer.Content = uc;
                    }
                }
            }
        }

        private void ModuleSelectDeviceExecute(object param)
        {
            //   ModuleContainer.Content = new System.Windows.Controls.Button() { Content = "ClickMe" };

            foreach (var item in this.ModulesPurchased)
            {
                if (param.ToString().Equals(item.Metadata["ModuleId"].ToString()))
                {
                    UserControl uc = item.Value as UserControl;
                    if (uc != null)
                    {
                        ModuleDeviceContainer.Content = null;
                        ModuleDeviceContainer.Content = uc;
                    }
                }
            }
        }

        private bool CanModuleSelectExecute(object param)
        {
            bool isAllowed = false;
            foreach (var item in this.ModulesPurchased)
            {
                if (param.ToString().Equals(item.Metadata["ModuleId"].ToString()))
                {
                    isAllowed = true;
                    break;
                }
                else
                {
                    isAllowed = false;
                }
            }
            return isAllowed;
        }

    }
}
